home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / tg-0.11 / istack.c < prev    next >
C/C++ Source or Header  |  2002-12-18  |  1KB  |  59 lines

  1. #include "alloc.h"
  2. #include "istack.h"
  3. #include "sysdefs.h"
  4.  
  5. #define     IS_CHUNK     256
  6.  
  7. extern void fatal( const char *msg );
  8.  
  9. struct istack {
  10.     int size, count;
  11.     int *data;
  12. };
  13.  
  14. struct istack *is_init( void )
  15. {
  16.     struct istack *s;
  17.     
  18.     s = (struct istack*)MALLOC( sizeof( *s ));
  19.     if ( s ) {
  20.         s->size = s->count = 0;
  21.         s->data = (int*)MALLOC( IS_CHUNK * sizeof(int) );
  22.         if ( !s->data ) {
  23.             FREE( s );
  24.             return 0;
  25.         }
  26.         
  27.         s->size = IS_CHUNK;
  28.     }
  29.     
  30.     return s;
  31. }
  32.  
  33. void is_push( struct istack *s, int n )
  34. {
  35.     if ( s ) {
  36.         if ( s->count >= s->size ) {
  37.             s->size += IS_CHUNK;
  38.             s->data = (int*)REALLOC( s->data, s->size * sizeof(int) );
  39.             if ( !s->data ) fatal( "stack overflow" );
  40.         }
  41.         
  42.         s->data[ s->count++ ] = n;
  43.     }
  44. }
  45.  
  46. int  is_top( struct istack *s )
  47. {
  48.     return (s->count > 0) ? s->data[ s->count-1 ] : 0;
  49. }
  50.  
  51. int  is_pop( struct istack *s )
  52. {
  53.     if ( s->count > 0 ) 
  54.         return s->data[ --s->count ];
  55.     
  56.     return 0;
  57. }
  58.  
  59.